package com.yingsheng.bi.hks.repository.dao.service;

import com.yingsheng.bi.hks.basic.consts.MsgDict;
import com.yingsheng.bi.hks.basic.enums.EnumConst;
import com.yingsheng.bi.hks.basic.exception.ServiceRuntimeException;
import com.yingsheng.bi.hks.basic.utils.Utils;
import com.yingsheng.bi.hks.repository.dao.mapper.PartyMapper;
import com.yingsheng.bi.hks.repository.dao.mapper.UserRoleMapper;
import com.yingsheng.bi.hks.repository.dao.mapper.UsersExtMapper;
import com.yingsheng.bi.hks.repository.dao.mapper.UsersMapper;
import com.yingsheng.bi.hks.repository.dao.po.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.support.TransactionTemplate;

import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * Users表的数据库操作服务
 * 要求说明：
 * 1.一般情况下,要求参数不超过5个时方法入参逐个声明
 * 2.一般情况下,要求条件不超过3个时要在方法名上体现
 * 3.最好对方法入参做一定校验,尤其是对条件参数至少要做非空校验
 *
 * @author root
 */
@Repository
public class UsersDaoService implements DaoService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UsersExtMapper usersExtMapper;

    @Autowired
    private PartyMapper partyMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    protected TransactionTemplate transactionTemplate;

    public Users registerUser(@NotNull String channel, @NotNull String mobile) {
        Users user = new Users();
        transactionTemplate.execute(status -> {
            Date current = new Date();
            Party party = new Party();
            party.setId(Utils.genUUid());
            party.setState(EnumConst.State.A);
            party.setType(EnumConst.UserType.P);
            party.setCreateTime(current);
            partyMapper.insertSelective(party);

            user.setId(Utils.genUUid());
            user.setState(EnumConst.State.A);
            user.setType(EnumConst.UserType.P);
            user.setMobile(mobile);
            user.setPartyId(party.getId());
            user.setChannelId(channel);
            user.setCreateTime(current);
            usersMapper.insertSelective(user);
            return null;
        });
        return user;
    }

    public void updateMobileByUserId(@NotNull String userId, @NotNull String mobile) {
        Users user = new Users();
        user.setId(userId);
        user.setMobile(mobile);
        user.setUpdateTime(new Date());
        usersMapper.updateByPrimaryKeySelective(user);
    }

    public Users selectByUserId(@NotNull String userId) {
        Users user = usersMapper.selectByPrimaryKey(userId);
        if (user == null) {
            throw new ServiceRuntimeException(MsgDict.TABLE_DATA_ERROR);
        }
        return user;
    }

    public Users selectByMobile(@NotNull String mobile) {
        UsersExample example = new UsersExample();
        example.createCriteria().andMobileEqualTo(mobile);
        Users user = usersMapper.selectByExample(example).get(0);
        return user;
    }

    public Users selectByChannelAndMobile(@NotNull String channel, @NotNull String mobile) {
        return usersExtMapper.selectUserByChannelAndMobile(channel, mobile);
    }

    public int updateUserRoleById(UserRole userRole){
        UserRoleExample example = new UserRoleExample();
        example.createCriteria().andUserIdEqualTo(userRole.getUserId());
        return userRoleMapper.updateByExampleSelective(userRole,example);
    }

    public int updateById(Users users){
        return usersMapper.updateByPrimaryKey(users);
    }
}
